package com.ld.control.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ld.control.business.process.model.ProcessNodeExampleModel;
import com.ld.control.business.process.model.ProcessNodeParamExampleModel;
import com.ld.control.sysenum.MethodParamTypeEnum;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
public class CommonUtils {

    private Integer getRobotPointName(Map<String, ProcessNodeParamExampleModel> processNodeParamExampleModeMap) {
        // 傲博机械臂
        ProcessNodeParamExampleModel processNodeParamExampleModel = processNodeParamExampleModeMap.get("robotPointName");
        String value = processNodeParamExampleModel.getValue();
        return Integer.valueOf(value);
    }

    public static String customizeNodeId() {
        return getUUID();
    }

    public static String getConditionNodeMapKey(ProcessNodeExampleModel processNodeExampleModel) {
        Long id = processNodeExampleModel.getId();
        Long processExampleId = processNodeExampleModel.getProcessExampleId();
        return  "ParallelNode_" + processExampleId + "_" + id;
    }

    public static String getParallelNodeMapKey(ProcessNodeExampleModel processNodeExampleModel) {
        Long id = processNodeExampleModel.getId();
        Long processExampleId = processNodeExampleModel.getProcessExampleId();
        return  "ParallelNode_" + processExampleId + "_" + id;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static String getPwdSalt() {
        return getUUID();
    }

    /**
     * 生成任务模板code
     * @param id
     * @return
     */
    public static String getTaskTmpCode(Long id) {
        long number = 100000000L;
        number = number + id;
        String numberStr = String.valueOf(number);
        String substring = numberStr.substring(1);
        return "TMP" + substring;
    }

    /**
     * 生产任务实例code
     * @param id
     * @return
     */
    public static String getTaskExampleCode(Long id) {
        long number = 100000000000000L;
        number = number + id;
        String numberStr = String.valueOf(number);
        String substring = numberStr.substring(1);
        return "EXAMPLE" + substring;
    }

    private static String objToString(Object obj) {
        return String.valueOf(obj);
    }

    public static Object getParamValue(String paramType, String value) {
        value = value.replaceAll("[\\r\\n]", "");
        try {
            if (MethodParamTypeEnum.INT.getType().equals(paramType)) {
                return getIntValue(value);
            }
            if (MethodParamTypeEnum.STRING.getType().equals(paramType)) {
                return getStringValue(value);
            }
            if (MethodParamTypeEnum.FLOAT.getType().equals(paramType)) {
                return getFloatValue(value);
            }
            if (MethodParamTypeEnum.DECIMAL.getType().equals(paramType)) {
                return getDecimalValue(value);
            }
            if (MethodParamTypeEnum.ARRAY.getType().equals(paramType)) {
                return getJsonArrayValue(value);
            }
            if (MethodParamTypeEnum.JSON_OBJECT.getType().equals(paramType)) {
                return getJsonObjectValue(value);
            }
        } catch (Exception e) {
            log.error("getParamValue error", e);
            throw new RuntimeException("getParamValue error:" + e.getMessage());
        }
        return null;
    }

    public static int getIntValue(String value) {
        return Integer.parseInt(value);
    }

    public static String getStringValue(String value) {
        return value;
    }

    public static float getFloatValue(String value) {
        return Float.parseFloat(value);
    }

    public static BigDecimal getDecimalValue(String value) {
        return new BigDecimal(value);
    }

    public static List<Integer> getIntArrayValue(String value) {
        return JSONObject.parseArray(value, Integer.class);
    }

    public static List<String> getStringArrayValue(String value) {
        return JSONObject.parseArray(value, String.class);
    }

    public static List<Float> getFloatArrayValue(String value) {
        return JSONObject.parseArray(value, Float.class);
    }

    public static List<BigDecimal> getBigDecimalArrayValue(String value) {
        return JSONObject.parseArray(value, BigDecimal.class);
    }

    public static JSONArray getJsonArrayValue(String value) {
        return JSONObject.parseArray(value);
    }

    public static JSONObject getJsonObjectValue(String value) {
        return JSONObject.parseObject(value);
    }

    public static void main(String[] args) {
        String array = "[\"aaa\", \"vvv\", \"bbb\", \"ccc\", \"ddd\", \"eee\", \"fff\"]";
        JSONArray jsonObject = JSONObject.parseArray(array);

        String array1 = "[1, 2, 3,]";
        JSONArray jsonObject1 = JSONObject.parseArray(array1);
        System.out.println();
    }

}
